package com.xmkj.bearbiz.activity;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.carbean.UidBean;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.entity.bean.UserBean;
import com.common.retrofit.methods.BindCarPhoneMethods;
import com.common.retrofit.methods.CheckRegisterCodeMethods;
import com.common.retrofit.methods.GetCodeMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.RegexUtils;
import com.common.utils.SPUtils;
import com.xmkj.bearbiz.MainActivity;
import com.xmkj.bearbiz.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/14.
 */

public class LoginBindPhoneActivity extends BaseMvpActivity {
    public static final String USERBEAN = "USERBEAN";
    public static final String AUTHID = "authid";
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView mTvGetCode;
    @BindView(R.id.tv_next_step)
    TextView mTvNextStep;
    //计时器
    private TimeCount timeCount;
    private UserBean userBean;
    private String authid;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_find_psw;
    }

    @Override
    protected void onViewCreated() {
        userBean = (UserBean) getIntent().getSerializableExtra(USERBEAN);
        authid = getIntent().getStringExtra(AUTHID);
    }

    @Override
    protected void doLogicFunc() {
        timeCount = new TimeCount(60000, 1000);//构造CountDownTimer对象
        attachClickListener(mTvNextStep);
        attachClickListener(mTvGetCode);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("绑定手机");
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == mTvNextStep.getId()) {
//            gotoActivity(ModifyPswActivity.class);
            if (EmptyUtils.isEmpty(getEditTextStr(etCode))) {
                showToastMsg("请输入验证码");
                return;
            }
            gotoCheck();
        } else if (view.getId() == mTvGetCode.getId()) {
            if (EmptyUtils.isEmpty(getEditTextStr(etPhone))) {
                showToastMsg("请输入手机号");
                return;
            } else if (!RegexUtils.isMobileExact(getEditTextStr(etPhone))) {
                showToastMsg("手机号不正确");
                return;
            }
            getCode();
        }
    }

    private void gotoCheck() {
        CommonSubscriber<UidBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();

//                Intent intent = new Intent(context, ModifyPswActivity.class);
//                intent.putExtra(ModifyPswActivity.SMSCODE, getEditTextStr(mEtCode));
//                intent.putExtra(ModifyPswActivity.MOBILE, getEditTextStr(mEtPhone));
//                startActivity(intent);
//                finish();
                bindPhone();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });


        CheckRegisterCodeMethods.getInstance().checkMobile(subscriber, getEditTextStr(etPhone), "3", getEditTextStr(etCode));
        rxManager.add(subscriber);
    }

    private void bindPhone() {
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                SPUtils.setShareInt(Constans.UID,userBean.uid);
                SPUtils.setShareString(Constans.HASHID,userBean.hashid);
                gotoActivity(MainActivity.class);
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });


        BindCarPhoneMethods.getInstance().MobileBind(subscriber,userBean.uid,userBean.hashid,authid,getEditTextStr(etPhone),getEditTextStr(etCode) );
        rxManager.add(subscriber);
    }

    private void getCode() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                showToastMsg("短信发送成功");
                timeCount.start();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });


        GetCodeMethods.getInstance().getCode(subscriber, getEditTextStr(etPhone), "3");
        rxManager.add(subscriber);
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }

    /**
     * 计时器
     * millisInFuture  总时长
     * countDownInterval 计时间隔
     */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mTvGetCode.setClickable(false);
            mTvGetCode.setText(millisUntilFinished / 1000 + "  秒");
        }

        @Override
        public void onFinish() {
//            btn_code.setBackgroundResource(R.drawable.btn_code);
            mTvGetCode.setText("重新发送");
            mTvGetCode.setClickable(true);
        }
    }
}
